<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\AdminRepo;
use App\Repos\RoleRepo;
use App\Models\Wxuser;
use App\Http\Requests\AdminRequest;

class AdminController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = AdminRepo::getList();
        $roles = RoleRepo::getList();
        $wxusers = Wxuser::all();
        return view('admin.admin.index',compact('list','roles','wxusers'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(AdminRequest $request)
    {
        $data = $request->pure();
        $salt = str_random(10);
        $data['salt'] = $salt;
        $data['password'] = md5($data['password'].$salt);
        $flag = AdminRepo::create($data);
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = AdminRepo::find($id);
        $roles = RoleRepo::getList();
        $wxusers = Wxuser::all();
        return view('admin.admin.edit',compact('data','roles','wxusers'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(AdminRequest $request, $id)
    {
        $data = $request->pure();
        if(array_has($data,'password')) {
            $salt = str_random(10);
            $data['salt'] = $salt;
            $data['password'] = md5($data['password'].$salt);    
        }        
        $flag = AdminRepo::update($id,$data);
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $flag = AdminRepo::destroy($id);
        return back();
    }
}
